<?php
class App_Service
{
    public static function handle_sign($uid){
        //首先查询这个用户是否已经签到过
        $query = DB::select()
                ->from('huiyuan_sign')
                ->where('uid', '=', $uid)
                ->where('date', '=', date('Y-m-d', time()))
                ->execute()
                ->as_array();
        if(!empty($query))
             return array('errorcode' => 'SUCC');
        
        //将签到结果插入到数据库
        list($insert_id, $rows) = DB::insert('huiyuan_sign')
            ->columns (array('uid', 'date'))
            ->values (array($uid, date('Y-m-d', time())))
            ->execute();

        return array('errorcode' => 'SUCC');
    }
    
    public static function handle_message ($post ,$uid)
    {
        $content = Arr::get($post, 'content');

        if (empty($content) )
        {
            return array('errorcode' => 'FAIL_EMPTY');
        }
        else 
        {
            list($insert_id, $rows) = DB::insert('huiyuan_message')
                ->columns (array('uid', 'content', 'ctime'))
                ->values (array($uid, $content, date('Y-m-d H:i:s', time())))
                ->execute();

            return array('errorcode' => 'SUCC');
        }
    }
    
    public static function handle_feedback ($post)
    {
        $content = Arr::get($post, 'content');
        $contact = Arr::get($post, 'contact');

        if (empty($content) || empty($contact))
        {
            return array('errorcode' => 'FAIL_EMPTY');
        }
        else 
        {
            list($insert_id, $rows) = DB::insert('service_feedback')
                ->columns (array('content', 'contact', 'created'))
                ->values (array($content, $contact, DB::expr('unix_timestamp(now())')))
                ->execute();

            return array('errorcode' => 'SUCC');
        }
    }

    public static function feedback_list ($page = 1)
    {
        $limit  = 50;
        $offset = (max($page, 1) - 1) * $limit;

        $query = DB::select()
            ->from('service_feedback')
            ->order_by('created', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->execute();

        return $query->as_array();
    }
}
